package com.mzba.zxdlyllp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import cn.waps.AppConnect;

import com.fortysevendeg.android.swipelistview.SwipeListView;
import com.mzba.zxdlyllp.db.BookTable;
import com.mzba.zxdlyllp.db.MasturbationTable;
import com.mzba.zxdlyllp.db.SexTable;
import com.mzba.zxdlyllp.db.SexTipsTable;
import com.mzba.zxdlyllp.db.SexualityTable;
import com.mzba.zxdlyllp.utils.DownloadUtils;
import com.mzba.zxdlyllp.utils.UICore;
import com.mzba.zxdlyllp.widget.ShowcaseView;
import com.mzba.zxdlyllp.widget.SlidingLayer;

public class MainActivity extends BasicActivity {
	
	private SlidingLayer mSlidingLayer;
	private ImageButton downloadBtn;
	private LayoutParams rlp;
	private ShowcaseView sv;
	
	private SwipeListView swipeListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		AppConnect.getInstance(this);	
		
		TextView iv = (TextView) findViewById(R.id.woman);
		iv.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, WaterfallActivity.class));
			}
		});
		TextView iv2 = (TextView) findViewById(R.id.masturbation);
		iv2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, MasturbationActivity.class));
			}
		});
		TextView iv3 = (TextView) findViewById(R.id.books);
		iv3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, BooksActivity.class));
			}
		});
		TextView iv4 = (TextView) findViewById(R.id.sex);
		iv4.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, SexPositionActivity.class));
			}
		});
		TextView iv5 = (TextView) findViewById(R.id.sex_tips);
		iv5.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, SextipsActivity.class));
			}
		});
		TextView iv6 = (TextView) findViewById(R.id.sexuality);
		iv6.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MainActivity.this, SexualityActivity.class));
			}
		});
		downloadBtn = (ImageButton) findViewById(R.id.download);
		downloadBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (sv != null && sv.isShown()) {
					int height = -900;
					if (CanvasHeight >= 1800) {
						height = -1500;
					} else if (CanvasHeight >= 1000) {
						height = -900;
					} else {
						height = -600;
					}
					int width = -600;
					if (CanvasWidth >= 1000) {
						width = -800;
					} else if (CanvasWidth >= 700) {
						width = -600;
					} else {
						width = -400;
					}
                    sv.animateGesture(0, 0, width, height);
                } else {
                	open();
                }
			}
		});
		
		mSlidingLayer = (SlidingLayer) findViewById(R.id.slidingLayer1);
		rlp = (LayoutParams) mSlidingLayer.getLayoutParams();
		mSlidingLayer.setStickTo(SlidingLayer.STICK_TO_BOTTOM);
		rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        rlp.width = LayoutParams.MATCH_PARENT;
        rlp.height = LayoutParams.MATCH_PARENT;
        mSlidingLayer.setLayoutParams(rlp);
        mSlidingLayer.setShadowWidth(0);
        mSlidingLayer.setShadowDrawable(null);
        mSlidingLayer.setOffsetWidth(0);
        
        String PREFS_NAME = "preferences";
		String FIRST_RUN_TAG = "first_run";
		SharedPreferences localSharedPreferences = getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
		SharedPreferences.Editor localEditor = localSharedPreferences.edit();
		if (!localSharedPreferences.getBoolean(FIRST_RUN_TAG, false)) {
			localEditor.putBoolean(FIRST_RUN_TAG, true);
			localEditor.commit();
			ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
            co.hideOnClickOutside = true;
            sv = ShowcaseView.insertShowcaseView(R.id.download, this, "操作提示", "点击下载按钮选择下载离线数据", co);
            sv.setOnShowcaseEventListener(new ShowcaseView.OnShowcaseEventListener() {
    			
    			@Override
    			public void onShowcaseViewShow(ShowcaseView showcaseView) {
    				
    			}
    			
    			@Override
    			public void onShowcaseViewHide(ShowcaseView showcaseView) {
    				
    			}
    		});
		}
        swipeListView = (SwipeListView) findViewById(R.id.lv_list);
//        swipeListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//        	swipeListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
//				
//				@Override
//				public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//					return false;
//				}
//				
//				@Override
//				public void onDestroyActionMode(ActionMode mode) {
//					
//				}
//				
//				@Override
//				public boolean onCreateActionMode(ActionMode mode, Menu menu) {
//					return false;
//				}
//				
//				@Override
//				public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
//					return false;
//				}
//				
//				@Override
//				public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
//					
//				}
//			});
//        	swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//        		@Override
//                public void onOpened(int position, boolean toRight) {
//        			
//                }
//
//                @Override
//                public void onClosed(int position, boolean fromRight) {
//                	
//                }
//
//                @Override
//                public void onListChanged() {
//                	
//                }
//
//                @Override
//                public void onMove(int position, float x) {
//                	
//                }
//
//                @Override
//                public void onStartOpen(int position, int action, boolean right) {
//                    Log.d("swipe", String.format("onStartOpen %d - action %d", position, action));
//                }
//
//                @Override
//                public void onStartClose(int position, boolean right) {
//                    Log.d("swipe", String.format("onStartClose %d", position));
//                }
//
//                @Override
//                public void onClickFrontView(int position) {
//                    Log.d("swipe", String.format("onClickFrontView %d", position));
//                }
//
//                @Override
//                public void onClickBackView(int position) {
//                    Log.d("swipe", String.format("onClickBackView %d", position));
//                }
//
//                @Override
//                public void onDismiss(int[] reverseSortedPositions) {
//                    
//                }
//        	});
//        }
        swipeListView.setAdapter(new SwipeListAdapter());
        
        TextView ad1 = (TextView) findViewById(R.id.ad1);
        ad1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AppConnect.getInstance(MainActivity.this).showGameOffers(MainActivity.this);
			}
		});
        TextView ad2 = (TextView) findViewById(R.id.ad2);
        ad2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AppConnect.getInstance(MainActivity.this).showAppOffers(MainActivity.this);
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		AppConnect.getInstance(this).finalize();
		super.onDestroy();
	}

	private void open() {
		 if (!mSlidingLayer.isOpened()) {
             mSlidingLayer.openLayer(true);
         }
	}
	
	public class SwipeListAdapter extends BaseAdapter {
		
		private String[] values = new String[5];
		
		public SwipeListAdapter() {
			values[0] = "两性常识";
			values[1] = "自慰技巧";
			values[2] = "避孕常识";
			values[3] = "异性交往";
			values[4] = "性爱宝典";
		}

		@Override
		public int getCount() {
			return values.length;
		}

		@Override
		public Object getItem(int arg0) {
			return values[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.swipelist_item, null);
			}
			((SwipeListView) parent).recycle(convertView, position);
			TextView tv = (TextView) convertView.findViewById(R.id.text);
			tv.setText(values[position]);
			Button downloadBtn = (Button) convertView.findViewById(R.id.download_data);
			downloadBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					UICore.eventTask(MainActivity.this, MainActivity.this, 0, position, true);
				}
			});
			Button deletedBtn = (Button) convertView.findViewById(R.id.delete_data);
			deletedBtn.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					switch (position) {
					case 0:
						SexTable st = new SexTable(MainActivity.this);
						st.clearTable();
						break;
					case 1:
						MasturbationTable mt = new MasturbationTable(MainActivity.this);
						mt.clearTable();
						break;
					case 2:
						BookTable bt = new BookTable(MainActivity.this);
						bt.clearTable();
						break;
					case 3:
						SexualityTable sut = new SexualityTable(MainActivity.this);
						sut.clearTable();
						break;
					case 4:
						SexTipsTable stt = new SexTipsTable(MainActivity.this);
						stt.clearTable();
						break;
					default:
						break;
					}
					Toast.makeText(MainActivity.this, "删除成功！", Toast.LENGTH_LONG).show();
				}
			});
			return convertView;
		}
		
	}
	
	@Override
	public void execute(int mes, Object obj) {
		try {
			DownloadUtils.download(Integer.parseInt(obj.toString()), this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ( keyCode == KeyEvent.KEYCODE_BACK) {
			if (mSlidingLayer.isOpened()) {
				mSlidingLayer.closeLayer(true);
			} else {
				return super.onKeyDown(keyCode, event);
			}
		}
		return false;
	}
}
